package Gui.Dialogs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

import Gui.CheckPanel;
import Gui.GameFrame;
import Gui.HorizontalButtonPanel;
import Main.Defines;

public class SellDealCardDialog extends JDialog {

	private Integer salesleft = 0;
	private CheckPanel cp = null;
	private HorizontalButtonPanel hbp = null;

	private ActionListener getSellButonAL() {
		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// If numchecked > numsells, uncheck all, do nothing

				if (cp != null) {
					if (cp.getNumberSelected() > salesleft) {
						cp.uncheckAll();
					} else {
						for (int i = Defines.CP().getDealCards().size() - 1; i >= 0; i--) {
							// for (int i = 0; i < 3; i++) {
							if (cp.isSelected(i)) {
								Defines.CP().AddCash(
										Defines.CP().getDealCards().get(i)
												.getValue());
								Defines.CP().getDealCards().remove(i);
								dispose();
								GameFrame.getInstance().update();
							}
						}
					}
				}
			}
		};
	}

	private ActionListener getDontSellAnythingButtonAL() {
		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
				GameFrame.getInstance().update();
			}
		};
	}

	private void buildUIContents(int numSales) {

		salesleft = numSales;
		
		JLabel L1 = new JLabel("Choose "
				+ (salesleft == 1 ? "a" : salesleft.toString()) + " deal"
				+ (salesleft > 1 ? "s" : "") + " to sell:");
		
		add(L1, BorderLayout.NORTH);
		

		if (Defines.CP().getDealCards().size() <= 0) {// this
			// if (false) {// that
			JLabel L2 = new JLabel("Sorry.  You don't have any.");
			add(L2, BorderLayout.CENTER);
		} else {
			// List the deal cards owned with checkboxes.
			String[] mydeals = new String[Defines.CP().getDealCards().size()];// this
			for (int i = 0; i < Defines.CP().getDealCards().size(); i++) {// this
				mydeals[i] = Defines.CP().getDealCards().get(i).toString();// this
			}// this
			// String[] mydeals = { "Zero", "Eno", "Owt" };// that
			cp = new CheckPanel(mydeals);
			add(cp.getPanel(), BorderLayout.CENTER);
		}

		String[] mybuttons = {
				"Sell",
				Defines.CP().getDealCards().size() > 0 ? "Ignore All Deals"
						: "OK" };
		ActionListener[] myfuncs = { getSellButonAL(),
				getDontSellAnythingButtonAL() };

		hbp = new HorizontalButtonPanel(mybuttons, myfuncs);

		if (cp == null) {
			hbp.getButton(0).setVisible(false);
		}
		add(hbp.getPanel(), BorderLayout.SOUTH);

	}

	public SellDealCardDialog(JFrame frame, int numSales) {
		super(frame, "Sell a Deal?", true);

		setLayout(new GridLayout(0, 1, 5, 5)); // MAGIC NUMBERS
		setLocationRelativeTo(GameFrame.getInstance());

		buildUIContents(numSales);

		pack();
		// REFACTOR THIS! MAGIC NUMBER
		if(getTitle() != null){
			FontMetrics fm = getFontMetrics(getFont());
			int width = fm.stringWidth(getTitle()) + 75; // MAGIC NUMBER, 75 is size of a "typical" window icon
			width = (int) Math.max(width*1.2, getPreferredSize().getWidth());
			setSize(new Dimension(width, getHeight()));
		}

		setVisible(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		// setSize((int) getSize().getWidth() * 2, (int) getSize().getHeight());
	}

	// public static void main(String[] args) {
	// JFrame f1 = new JFrame("Frame");
	// f1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	//
	// JDialog jd = new SellDealCardDialog(f1, 1);
	//
	// f1.setVisible(true);
	// f1.pack();
	// }
}
